using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Configuration;
using System.Reflection;

namespace POSSystem.BLL
{
    public static class ErrorLog
    {

        public static string WriteToFile(string fname, string cname, string error)
        {
            string ret = string.Empty;
            string heading = string.Empty;
            string addrow = string.Empty;
            // Specify file, instructions, and privelegdes
            try
            {
                string FilePath = string.Empty;
                if (HttpContext.Current != null)
                {
                    FilePath = WebConfigurationManager.AppSettings["LogFile"];
                    if (FilePath.StartsWith("~"))
                        FilePath = HttpContext.Current.Server.MapPath(FilePath);
                }
                else
                {
                    FilePath = ConfigurationManager.AppSettings["LogFile"];
                    FilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + FilePath;
                    //FilePath = Assembly.GetExecutingAssembly().CodeBase + FilePath;
                }
                FileStream file;
                heading = "File Name          ; Class Name    ; Error Description        ; Date             ";

                if (!File.Exists(FilePath))
                {
                    file = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write);
                }
                else
                {
                    file = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                    long fileSize = file.Length;
                    file.Close();
                    if (fileSize >= 204800)
                    {
                        int lastIndex = FilePath.LastIndexOf(".");
                        string newFilePath = string.Empty;
                        if (lastIndex > 0)
                        {
                            string ext = FilePath.Substring(lastIndex);
                            newFilePath = FilePath.Substring(0, lastIndex);
                            newFilePath += "_" + DateTime.Now.ToString("yyyy_MMM_dd_HH_mm_ss") + ext;
                        }
                        else
                            newFilePath = FilePath + ".bak" + DateTime.Now.ToString("yyyy_MMM_dd_HH_mm_ss");
                        File.Move(FilePath, newFilePath);
                        file = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write);
                    }
                    else
                        file = new FileStream(FilePath, FileMode.Append, FileAccess.Write);

                }
                //FileStream file = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write);

                // Create a new stream to write to the file
                StreamWriter sw = new StreamWriter(file);

                if (!string.IsNullOrEmpty(heading))
                    sw.WriteLine(heading);
                sw.WriteLine("===============================================================================");
                addrow = fname + " ; " + cname + " ; " + error + " ; " + DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                // Write a string to the file
                sw.WriteLine(addrow);

                // Close StreamWriter
                sw.Close();

                // Close file
                file.Close();
                ret = "Successfull";
            }
            catch (Exception ex)
            {
                ret = "Error : " + ex.Message;

            }

            return ret;
        }
    }
}

